<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="设备ID（关联设备台账）" prop="equipmentId">
                        <el-input v-model.number="dataForm.equipmentId" placeholder="设备ID（关联设备台账）"></el-input>
                </el-form-item>
                <el-form-item label="评估周期开始日期" prop="evaluationPeriodStart">
                        <el-date-picker
                                v-model="dataForm.evaluationPeriodStart"
                                type="datetime"
                                placeholder="评估周期开始日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="评估周期结束日期" prop="evaluationPeriodEnd">
                        <el-date-picker
                                v-model="dataForm.evaluationPeriodEnd"
                                type="datetime"
                                placeholder="评估周期结束日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="评估前故障率" prop="failureRateBefore">
                        <el-input v-model="dataForm.failureRateBefore" placeholder="评估前故障率"></el-input>
                </el-form-item>
                <el-form-item label="评估后故障率" prop="failureRateAfter">
                        <el-input v-model="dataForm.failureRateAfter" placeholder="评估后故障率"></el-input>
                </el-form-item>
                <el-form-item label="评估前维修成本" prop="repairCostBefore">
                        <el-input v-model="dataForm.repairCostBefore" placeholder="评估前维修成本"></el-input>
                </el-form-item>
                <el-form-item label="评估后维修成本" prop="repairCostAfter">
                        <el-input v-model="dataForm.repairCostAfter" placeholder="评估后维修成本"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="删除时间（软删除）" prop="deletedAt">
                        <el-date-picker
                                v-model="dataForm.deletedAt"
                                type="datetime"
                                placeholder="删除时间（软删除）">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            equipmentId: null,
            evaluationPeriodStart: null,
            evaluationPeriodEnd: null,
            failureRateBefore: null,
            failureRateAfter: null,
            repairCostBefore: null,
            repairCostAfter: null,
            createdAt: null,
            updatedAt: null,
            deletedAt: null
          },
          dataRule: {
            id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
            ],
            equipmentId: [
                        { required: true, message: '设备ID（关联设备台账）不能为空', trigger: 'blur' }
            ],
            evaluationPeriodStart: [
                        { required: true, message: '评估周期开始日期不能为空', trigger: 'blur' }
            ],
            evaluationPeriodEnd: [
                        { required: true, message: '评估周期结束日期不能为空', trigger: 'blur' }
            ],
            failureRateBefore: [
                        { required: true, message: '评估前故障率不能为空', trigger: 'blur' }
            ],
            failureRateAfter: [
                        { required: true, message: '评估后故障率不能为空', trigger: 'blur' }
            ],
            repairCostBefore: [
                        { required: true, message: '评估前维修成本不能为空', trigger: 'blur' }
            ],
            repairCostAfter: [
                        { required: true, message: '评估后维修成本不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            deletedAt: [
                        { required: true, message: '删除时间（软删除）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.id = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/app/equipment_maintenance_effect/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appequipmentmaintenanceeffect
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/equipment_maintenance_effect/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>